'''
2数之和：

遍历数组，把每个数字加入到hash表，如果target-新的值的结果在hash表中，则返回其索引以及新值的值。
'''

def twoSum(nums, target):
    num_to_index = {}
    for i, num in enumerate(nums):
        print(f"i={i}, num={num}")
        complement = target - num
        if complement in num_to_index:
            print(f"complement={complement}, num={num_to_index[complement]}")
            return [num_to_index[complement], i]
        num_to_index[num] = i
    return []

# 测试用例
def test_twoSum():
    # 测试用例 1
    nums1 = [2, 7, 11, 15]
    target1 = 9
    print(f"Test case 1: {twoSum(nums1, target1)}")  # 预期输出: [0, 1]

    # 测试用例 2
    nums2 = [3, 2, 4]
    target2 = 6
    print(f"Test case 2: {twoSum(nums2, target2)}")  # 预期输出: [1, 2]

    # 测试用例 3
    nums3 = [3, 3]
    target3 = 6
    print(f"Test case 3: {twoSum(nums3, target3)}")  # 预期输出: [0, 1]

    # 测试用例 4
    nums4 = [0, 4, 3, 0]
    target4 = 0
    print(f"Test case 4: {twoSum(nums4, target4)}")  # 预期输出: [0, 3]

    # 测试用例 5
    nums5 = [-1, -2, -3, -4, -5]
    target5 = -8
    print(f"Test case 5: {twoSum(nums5, target5)}")  # 预期输出: [2, 4]

# 运行测试用例
test_twoSum()